package com.campusmarket.controller;


import com.campusmarket.common.base.BaseController;
import com.campusmarket.common.utils.FormUtils;
import com.campusmarket.dto.LayuiDataGridResult;
import com.campusmarket.dto.LayuiResult;
import com.campusmarket.model.Commodity;
import com.campusmarket.model.User;
import com.campusmarket.service.CommodityService;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/commodity")
public class CommodityController extends BaseController {

    @Autowired
    private CommodityService commodityService;

    @ModelAttribute("commodity")
    public Commodity get(@RequestParam(required = false)Long id) throws Exception{
        if(id != null && id > 0){
            return commodityService.queryById(id);
        }else{
            return new Commodity();
        }
    }

    @RequestMapping(value = "/add_commodity")
    public String add_commodity(Commodity commodity,HttpSession session) throws Exception {
        User user=(User)session.getAttribute("currentUser");
        commodity.setUserId(user.getId());
        Commodity flag=commodityService.insert(commodity);
        if(flag!=null){
            log.info("添加成功!");
            //跳转到XX界面
            if(user.getUserName().equals("admin")) return "interface/adminstrator";
            else return "interface/user";
        }else {
            log.info("添加失败!");
            return "interface/administrator";
        }
    }

    @RequestMapping(value = "/del_commodity",produces = "application/json;charset=utf-8",method = RequestMethod.POST)
    @ResponseBody
    public LayuiResult del_commodity(Long id) throws Exception {
        LayuiResult result=new LayuiResult();
        try{
            commodityService.delete(id);
            result.setStatus(LayuiResult.OK);
            result.setMsg("删除成功!");
        }catch(Exception e){
            result.setStatus(LayuiResult.FAIL);
            result.setMsg("删除商品时发生异常!");
        }
        return result;
    }

    @RequestMapping("/query_commodity")
    public String query_commodity(Long id) throws Exception {
        Commodity flag=commodityService.queryById(id);
        if(flag!= null){
            log.info("查找成功!");
            return "interface/user_check";
        }else {
            log.info("查找失败!");
            return "interface/user_check";
        }
    }

    @RequestMapping("/update_commodity")
    public String update_commodity(Commodity commodity) throws Exception {
        Commodity flag=commodityService.update(commodity);
        if(flag!=null){
            log.info("修改成功!");
            //更新到数据库
            commodityService.update(commodity);
            return "interface/user_check";
        }else {
            log.info("修改失败!");
            return "interface/user_check";
        }
    }

    @RequestMapping(value="/list.json",produces = "application/json;charset=utf-8",method = RequestMethod.POST)
    @ResponseBody
    public LayuiDataGridResult list(int limit, int page,String key, HttpSession session) throws Exception{
        User user=(User)session.getAttribute("currentUser");
        LayuiDataGridResult result=commodityService.selectListByPage(page,limit,key,null);
        return result;
    }


    @RequestMapping(value="/mylist.json",produces = "application/json;charset=utf-8",method = RequestMethod.POST)
    @ResponseBody
    public LayuiDataGridResult mylist(int limit, int page,String key, HttpSession session) throws Exception{
        User user=(User)session.getAttribute("currentUser");
        LayuiDataGridResult result=commodityService.selectListByPage(page,limit,key,user);
        return result;
    }

    /**
     * 处理更新的请求
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value={"/add", "/edit"},method = RequestMethod.POST)
    public String form(@ModelAttribute("commodity") Commodity commodity, HttpSession session,Model model) throws Exception{
        User user=(User)session.getAttribute("currentUser");
        if(commodity.getId()!=null&&commodity.getId()>0){
            commodityService.update(commodity);
        }else{
            commodity.setUserId(user.getId());
            commodityService.insert(commodity);
        };
        User currentUser=(User)session.getAttribute("currentUser");
        //admin账号默认为管理员
        if(currentUser!=null&&currentUser.getUserName().equals("admin")){
            return "interface/admin_operation_com";
        }else{
            return "interface/user_check";
        }
    }

}
